﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.WebModel;
using TrainingsApp.Model;
using TrainingsApp.Logic;

namespace TrainingsApp.IPADApp.UserControls2.Teacher
{
    public partial class UC_Preview_QuickQuery : AccountControl
    {
        protected int ClassCount { get; set; }

        private Guid pid
        {
            get { return new Guid(Request.QueryString["p"]); }
        }

        private long cid
        {
            get { return Convert.ToInt64(Request.QueryString["c"]); }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            TS_Preview_Entity qe = TS_Preview_Logic.TS_Preview_ViewGroupByPreviewId(pid);
            ltTitle.Text = qe.Question;
            ltCount.Text = qe.ResultCount.ToString();
            BindClasses();
            BindData();
        }

        private void BindClasses()
        {
            List<TS_Class_Entity> list = TS_Class_Logic.TS_Class_GetClassesByUserId(this.CurAccountPage.CurrentUser.UserID);
            rpClass.DataSource = list;
            ClassCount = list.Count;
            rpClass.DataBind();
        }

        private void BindData()
        {
            List<TS_Preview_Entity> list = TS_Preview_Logic.TS_Preview_PreviewGroupByTeacher(pid);
            rpTeacher.DataSource = list;
            rpTeacher.DataBind();
        }

        protected void rpTeacher_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rp = e.Item.FindControl("rpClasses") as Repeater;
                TS_Preview_Entity entity = (TS_Preview_Entity)e.Item.DataItem;
                Dictionary<string, object> dict = new Dictionary<string, object>();
                dict.Add("TeachId", PassPort.CurrentUser.UserID);
                dict.Add("QuestionId", entity.QuestionId.Value);
                dict.Add("ChapterId", cid);
                rp.DataSource = TS_Preview_Logic.TS_Preview_GetClassesByQuestionId(dict);
                rp.DataBind();
            }
        }
    }
}